package com.olympus.hermes.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * 网关统一配置跨域
 * @author Mickey
 * @since 2021/1/21
 */
@Configuration
public class CrossConfig {

    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //所有的请求头
        corsConfiguration.addAllowedHeader("*");
        //所有的请求方式
        corsConfiguration.addAllowedMethod("*");
        //允许任意请求来源
        corsConfiguration.addAllowedOriginPattern("*");
        //允许请求携带cookie信息
        corsConfiguration.setAllowCredentials(true);
        //所有路径
        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsWebFilter(source);
    }
}
